<template>
  <div class="footer">
    <!-- <div class="links">
      <a href="http://www.jeecg.com" target="_blank">JEECG 首页</a>
      <a href="https://github.com/zhangdaiscott/jeecg-boot" target="_blank">
        <a-icon type="github"/>
      </a>
      <a href="https://ant.design/">Ant Design</a>
      <a href="https://vuecomponent.github.io/ant-design-vue/docs/vue/introduce-cn/">Vue Antd</a>
    </div> -->
    <div class="copyright">
      ICP备案/许可证号:黑ICP备2024026134号-1&nbsp;&nbsp;黑龙江省启明码源科技开发有限责任公司&nbsp;&nbsp;业务联系方式：13046176666
      <!-- Copyright
      <a-icon type="copyright"/>
      2019 <span>JEECG开源社区 出品</span> -->
    </div>
  </div>
</template>

<script>
  export default {
    name: "LayoutFooter",
    data(){
      return {
        lockReconnect:false,
        isChange:false,
        websock1:null
      }
    },
    mounted() {
      // debugger
      console.log('ggggggggggggggggggggfffffffffffffffffffffffffffffffffffffffffffffffff')
      this.initWebSocket();
    },
    destroyed(){
      console.log('xiaohuimeiyoulelianjie销毁呢')
      this.websock1.onmessage = null;
      this.websock1.onerror = null;
      this.websock1.close(1000, "安全关闭");
      this.websock1 = null;
        console.log('88888888888888888888888医疗链接销毁呢')
    },
    methods:{
      initWebSocket: function () {
      this.websock1 = new WebSocket('ws://127.0.0.1:23011?token=梅里斯达斡尔族区');
      this.websock1.onopen = this.websocketOnopen;
      this.websock1.onerror = this.websocketOnerror;
      this.websock1.onmessage = this.websocketOnmessage2;
      this.websock1.onclose = this.websocketOnclose;
    },
    websocketOnopen: function () {
      console.log('88888888888888888888888医疗连接成功呢')
    },
    websocketOnerror: function (e) {
      this.reconnect();
    },
    websocketOnmessage2:function (event){
      let that = this
      let res = event.data
      let result = JSON.parse(res)
      if(result.code == 200){
        if(this.isChange){
          return
        }
        this.isChange = true
        console.log('ggggggggggggggggggggggfffffffffffffffffffrrrrrrrrrrrrrrrrrrrrrrrrr')
        let dat = result.data
        let name = result.type==2?'处方':'病例'
        if(result.type!=1){
            this.$confirm({
              title: '智慧助手',
              content: `检测到${name}回传,是否覆盖当前${name}？`,
              centered: true,
              onOk: () => {
                that.isChange = false
                if(result.type==2){ //处方回传
                  this.$bus.$emit('websocketMessageChuFang',dat)
                  let obj = {
                    code:200,
                    msg:"接收处方成功",
                    sessionId:result.sessionId
                  }
                  this.websocketSend(obj)
                } else if(result.type==3) {
                  let obj = {
                    code:200,
                    msg:"接收病例",
                    sessionId:result.sessionId
                  }
                  this.websocketSend(obj)
                  this.$bus.$emit('websocketMessage',dat)
                }
              },
              onCancel() {
                that.isChange = false
              },
            })
        }  else{
            let obj = {
              code:200,
              msg:"接收审方消息成功",
              sessionId:result.sessionId
            }
            this.websocketSend(obj)
            this.$bus.$emit('websocketMessageShenFang',result)
            this.isChange = false
        }
        
      }
    },
    websocketOnclose: function (e) {
      // this.reconnect();
    },
    websocketSend(text) {
      // 数据发送
      try {
        this.websock.send(text);
      } catch (err) {
        console.log("send failed (" + err.code + ")");
      }
    },
    reconnect() {
      var that = this;
      if(that.lockReconnect) return;
      that.lockReconnect = true;
      that.websockTime +=1
      //没连接上会一直重连，设置延迟避免请求过多
      setTimeout(function () {
        that.initWebSocket();
        that.lockReconnect = false;
      }, 5000);
      
    },
    }
  }
</script>

<style lang="less" scoped>
  .footer {
    padding: 0 16px;
    margin: 5px 0;
    text-align: center;

    .links {
      margin-bottom: 8px;

      a {
        color: rgba(0, 0, 0, .45);

        &:hover {
          color: rgba(0, 0, 0, .65);
        }

        &:not(:last-child) {
          margin-right: 40px;
        }
      }
    }
    .copyright {
      color: rgba(0, 0, 0, .45);
      font-size: 14px;
    }
  }
</style>